package by.grsu.shakh.page.librarian;

import java.util.List;

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;

import by.grsu.shakh.app.LibraryWebApplication;
import by.grsu.shakh.dao.CatalogDao;
import by.grsu.shakh.entity.Catalog;

/**
 * list page for Catalog object.
 * 
 * @author shakh_andrew
 */
public class ListCatalogPage extends WebPage {

	public ListCatalogPage() {
		super();

		final CatalogDao catalogDao = new CatalogDao(
				LibraryWebApplication.DATA_XML_FOLDER);

		final List<Catalog> allCatalog = catalogDao.getAll();

		add(new DataView<Catalog>("table-rows", new ListDataProvider<Catalog>(
				allCatalog)) {
			@Override
			protected void populateItem(final Item<Catalog> item) {
				final Catalog catalog = item.getModelObject();

				item.add(new Label("id", catalog.getId()));
				item.add(new Label("name", catalog.getName()));

				item.add(new Link("editLink") {
					@Override
					public void onClick() {
						setResponsePage(new CatalogEditPage(catalog));
					}
				});

				item.add(new Link("deleteLink") {
					@Override
					public void onClick() {
						catalogDao.delete(catalog.getId());
						setResponsePage(new ListCatalogPage());
					}
				});
			}
		});

		add(new Link("createLink") {
			@Override
			public void onClick() {
				setResponsePage(new CatalogEditPage(new Catalog()));
			}
		});
	}
}
